package com.da.web.dorm_room.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.da.common.StatusCode;
import com.da.exception.MyException;
import com.da.web.dorm_room.entity.DormRoom;
import com.da.web.dorm_room.entity.RoomParm;
import com.da.web.dorm_room.entity.RoomTree;
import com.da.web.dorm_room.mapper.DormRoomMapper;
import com.da.web.dorm_room.service.DormRoomService;
import com.da.web.dorm_room_bed.entity.DormRoomBed;
import com.da.web.dorm_room_bed.service.DormRoomBedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author da
 * @time 2023/12/9 下午 5:05
 */
@Service
public class DormRoomServiceImpl extends ServiceImpl<DormRoomMapper, DormRoom> implements DormRoomService {
    @Autowired
    private DormRoomBedService dormRoomBedService;

    @Override
    public List<RoomTree> getRoomTree(Long storeyId) {
        //根据层数id查询宿舍
        List<RoomTree> roomTree = this.baseMapper.getRoomTree(storeyId);
        for (RoomTree tree : roomTree) {
            //根据宿舍id查询对应的床位
            Long roomId = tree.getRoomId();
            tree.setType("1"); // 这一级是宿舍
            //床位列表
            List<DormRoomBed> bedList = dormRoomBedService.getBedList(roomId);
            for (DormRoomBed bed : bedList) {
                RoomTree room = new RoomTree();
                room.setRoomId(bed.getBedId());
                room.setRoomCode(bed.getBedCode());
                room.setType("0"); // 这一级是床位
                //把床位放到宿舍
                tree.getChildren().add(room);
            }
        }
        return roomTree;
    }

    @Transactional
    @Override
    public boolean addRoom(RoomParm parm) {
        //起始编号
        Integer start = parm.getStart();
        Integer end = parm.getEnd();
        if (start <= 0 || end <= 0 || start >= end) {
            throw new MyException(StatusCode.ERROR_CODE, "开始编号或者结束编号有误");
        }
        Long storeyId = parm.getStoreyId();
        if (storeyId == null) {
            throw new MyException(StatusCode.ERROR_CODE, "楼栋id不能为空");
        }
        List<DormRoom> rooms = new ArrayList<>();
        while (start <= end) {
            // 实体类创建
            DormRoom room = new DormRoom();
            room.setTotalBed(parm.getStuNum());
            room.setRoomType(parm.getRoomType());
            room.setStoreyId(parm.getStoreyId());
            //构造编号
            if (StrUtil.isNotEmpty(parm.getArea())) {
                room.setRoomCode(parm.getArea() + start); // A100...
            } else {
                room.setRoomCode(String.valueOf(start));
            }
            QueryWrapper<DormRoom> query = new QueryWrapper<>();
            //楼栋对应的宿舍编号不能重复
            query.lambda().eq(DormRoom::getRoomCode, room.getRoomCode())
                    .eq(DormRoom::getStoreyId, room.getStoreyId());
            DormRoom one = this.getOne(query);
            //数据库里面不存在时才添加
            if (one == null) {
                rooms.add(room);
            } else {
                throw new MyException(StatusCode.ERROR_CODE, "编号重复 [ " + room.getRoomCode() + " ]");
            }
            start++;
        }
        //保存房间
        boolean b = false;
        if (!rooms.isEmpty()) {
            b = this.saveBatch(rooms);
        }
        return b;
    }
}
